<template>
  <el-dialog title="选择在途状态信息" :close-on-click-modal="false" :visible.sync="visible" fullscreen center append-to-body>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>

    <el-button type="primary" @click="checkStatusData" style="margin-bottom: 5px;">选择并关闭</el-button>
    <el-table ref="multipleTable" :data="saleStatusOption" border tooltip-effect="dark" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="80"></el-table-column>
      <el-table-column prop="code" label="在途状态编码"></el-table-column>
      <el-table-column prop="name" label="在途状态"></el-table-column>
    </el-table>
  </el-dialog>
</template>

<script>
  import { getSaleStatus } from "@/api/eims/optician/glassesinfo";

  export default {
    data() {
      return {
        visible: false,
        saleStatusOption: [],
        multipleSelection: []
      }
    },
    methods: {
      init(list) {
        this.visible = true
        this.saleStatusOption = []
        this.multipleSelection = list
        this.dataList = list
        this.getOpticianStatus()
      },

      // 获取配镜单在途状态列表
      getOpticianStatus() {
        getSaleStatus().then(response => {
          if(response.data.code == '0'){
            this.saleStatusOption = response.data.data
            this.$nextTick(() => {
              this.dataList.forEach(item => {
                for (let i = 0; i < this.saleStatusOption.length; i++) {
                  if (item.id == this.saleStatusOption[i].id) {
                    this.$refs.multipleTable.toggleRowSelection(this.saleStatusOption[i], true)
                  }
                }
              })
            })
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      handleSelectionChange(val){
        val.sort(this.sortByCode('code'))
        this.multipleSelection = val
      },
      // 选择在途状态数据并关闭弹窗
      checkStatusData(){
        this.visible = false
        this.$emit('refreshDataList',this.multipleSelection)
      },
      // 排序规则函数
      sortByCode(code){
        return function(obj1,obj2){
          let value1 = obj1[code]
          let value2 = obj2[code]
          return value1 - value2
        }
      }
    }
  }
</script>
